<?php

class UserController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
        require_once('Zend/Session.php');
        $session = new Zend_Session_Namespace('product');
        if (!isset($session->products)) {
            $session->products = array();
        }
    }
    public function  preDispatch() {
        $auth= Zend_Auth::getInstance();
        $login = $auth->getStorage()->read();
        if (! $auth->hasIdentity()) {
           $this->_helper->redirector('index', 'auth');
        }else {
            if($login->role != 'member'){
                $this->_helper->redirector('index', 'admin');
            }else{

                parent::preDispatch();
            }
        }
    }

    public function indexAction() {
        // action body
    }

    public function showAction()
    {
        $usr = new APP_Form_ShowForm();
        $this->view->form = $usr;
        
        $auth = Zend_Auth::getInstance();
        $login = $auth->getStorage()->read();
                
        $users = new Zend_Db_Table('users');
        $usersQuery = $users->fetchRow("email='".$login->email."'");
        $this->view->row = $usersQuery;
        
    }
    
    public function editAction()
    {
        $edit = new APP_Form_EditForm();
        $this->view->form = $edit;
        
        $auth = Zend_Auth::getInstance();
        $login = $auth->getStorage()->read();
                
        $users = new Zend_Db_Table('users');
        $usersQuery = $users->fetchRow("email='".$login->email."'");
        $this->view->row = $usersQuery;
        
        $request = $this->getRequest();
        if($request->isPost())
        {
            if($edit->isValid($_POST))
            {
                $date = new Zend_Db_Expr('CURDATE()');
                $users = new Zend_Db_Table('users');
                $users->update(array('user_name'=>$_POST['userName'],
                    'job'=>$_POST['job'],
                    'birthday'=>$_POST['bdate'],
                    'address'=>$_POST['add'],
                    'country'=>$_POST['country'],
                    'credit_limit'=>$_POST['crdtLmt'],
                    'creadit_card_number'=>$_POST['crdtNum'], 'updated_at'=>$date), "email='".$login->email."'");
                
            }
        }
    }

    public function activeAction()
    {
        
        
    }
    

    
    public function userpageAction() {
        $this->product();
    }

    public function productajaxAction() {
        // action body
        if (isset($_GET['proid'])) {
            $session = new Zend_Session_Namespace('product');
            array_push($session->products, $_GET['proid']);
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
        } else if (isset($_GET['category'])) {
            $this->product();
            $this->_helper->layout->disableLayout();
        }
    }

    protected function product() {
        if (isset($_GET['category'])) {
            $catid = (int) $_GET['category'];
        } else {
            $catid = 1;
        }
        $products = new Zend_Db_Table('products');
        $category = new Zend_Db_Table('category');
        $cat = $category->fetchRow("category_id=" . $catid);
        $pro = $products->fetchAll("cat_id=" . $catid);
        $this->view->allproducts = $pro;
        $this->view->categoryname = $cat['category_name'];
    }

    public function displaycartAction() {
        $this->display();
    }

    public function removefromcartajaxAction() {

        $session = new Zend_Session_Namespace('product');
        $pro = (int) $_POST['pro_id'];
        $index = (int) array_search($pro, $session->products);
        unset($session->products[$index]);
        $this->display();
        $this->_helper->layout->disableLayout();
    }

    protected function display() {

        $session = new Zend_Session_Namespace('product');
        $auth = Zend_Auth::getInstance();
        $login = $auth->getStorage()->read();
        $myuser = new Zend_Db_Table('users');
        $user = $myuser->fetchRow("email=" . "'" . $login->email . "'");

        if (!empty($session->products)) {
            foreach ($session->products as $selected_product) {
                $product = new Zend_Db_Table('products');
                $pro = $product->fetchRow("product_id=" . $selected_product);
                $selectedproducts[] = $pro;
            }
            $this->view->allselectedproducts = $selectedproducts;
            $this->view->message = "The selected products";
            $this->view->check = "1";
        } else {

            $selectedproducts[] = $pro;
            $this->view->message = "you havent choose any product";
            $this->view->check = "0";
        }
    }

    public function buyajaxAction() {

        $session = new Zend_Session_Namespace('product');

        if (!empty($session->products)) {
            $bought = $session->products;
            $auth = Zend_Auth::getInstance();
            $login = $auth->getStorage()->read();
            $myuser = new Zend_Db_Table('users');
            $user = $myuser->fetchRow("email=" . "'" . $login->email . "'");

            foreach ($bought as $selected_product) {

                $products = new Zend_Db_Table('cart');
                $new = $products->createRow();
                $new->user_id = (int) $user['user_id'];
                $new->product_id = (int) $selected_product;
                $new->status = "bought";
                $new->quantity = 1;
                $new->date = strftime("%G-%m-%e", strtotime("now"));
                $new->time = date("h:i:s A");
                $new->save();
            }
            Zend_Session::namespaceUnset('product');
            $this->view->message = "have be done succssfully";
        } else {

            $this->view->message = "No products have been chosen";
        }
        $this->_helper->layout->disableLayout();
    }

}
